from datetime import time

from selenium import webdriver
from selenium.webdriver import ActionChains
from selenium.webdriver.support import expected_conditions as EC
from selenium.webdriver.support.ui import WebDriverWait


class Base():
    def open_url(self, url):
        self.driver = webdriver.Edge()
        self.driver.get(url)

    def find_element(self, loc):
        try:
            WebDriverWait(self.driver, 20, 0.5).until(EC.presence_of_element_located(*loc))
            return self.driver.find_element(*loc)
        except Exception as e:
            print(e)

    def click(self, loc):
        self.find_element(loc).click()

    def send_keys(self, loc, test):
        self.find_element(loc).send_keys(test)

    def alertHandle(self):
        a = self.driver.switch_to.alert
        a.accept()

    def xuanting(self, loc):
        ele = self.find_ele(loc)
        ActionChains(self.driver).move_to_element(ele).perform()

    def jiepig(self, filename):
        t = time.strftime("%Y%m%d%H%M%S", time.localtime())
        self.driver.save_screenshot(t + filename)
